﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScheduleMaker.ConfigManager;
using ScheduleMaker.DB;
using ScheduleMaker.DB.Domain;

namespace ScheduleMaker
{
    public partial class smCreateConfig : Form
    {
        private smConfigFile parent;

        public smCreateConfig(smConfigFile parent)
        {
            InitializeComponent();
            this.parent = parent;
            foreach(var modeName in Enum.GetValues(typeof(Mode)))
            {
                modeComboBox.Items.Add(modeName);
            }
        }

        private void createButton_Click(object sender, EventArgs e)
        {
            String addr = addrTextBox.Text;
            String dbName = dbNameText.Text;
            String login = loginTextBox.Text;
            String pass = passTextBox.Text;
            Mode mode = (Mode)modeComboBox.SelectedItem;

            String adminLogin = adminLoginTextBox.Text;
            String adminPass = adminPassTextBox.Text;

            Config config = new Config(addr + "//" + dbName, login, pass, mode);

            ConfigManager.ConfigManager.setConfiguration(config);
            if (createTableCheckBox.Checked)
            {
                generateTable(addr, dbName, login, pass, mode);
                createLevels();
            }
            if (createUserCheckBox.Checked)
            {
                createUser(adminLogin, adminPass);
            }
            ConfigManager.ConfigManager.saveConfiguration(adminPass, config);
            openLoginForm();
        }

        private void openLoginForm()
        {
            Form smLogin = new smLoginForm();
            smLogin.FormClosed += new FormClosedEventHandler(closeForm);
            smLogin.FormClosed += new FormClosedEventHandler(parent.closeForm);
            smLogin.Show();
            this.Hide();
            parent.Hide();
        }

        private void generateTable(String addr, String dbName, String login, String pass, Mode mode)
        {
            TableCreator tableCreator = new TableCreator(addr, dbName, login, pass);
            switch (mode)
            {
                case Mode.School:
                    tableCreator.generate(TableCreator.GENERATE_SCHOOL_TABLES);
                    break;
                case Mode.University:
                    tableCreator.generate(TableCreator.GENERATE_UNIVERSITY_TABLES);
                    break;
            }
        }

        private void createLevels()
        {

            LevelDAOSchool levelDAO = new LevelDAOSchool();
            LevelSchool admin = new LevelSchool();
            admin.Name = "ADMIN";
            LevelSchool teacher = new LevelSchool();
            teacher.Name = "TEACHER";
            LevelSchool user = new LevelSchool();
            user.Name = "USER";
            levelDAO.save(admin);
            levelDAO.save(teacher);
            levelDAO.save(user);
        }

        private void createUser(String login, String pass)
        {
            UserDAOSchool userDAO = new UserDAOSchool();
            UserSchool user = new UserSchool();
            user.Login = login;
            user.Password = MD5Coder.encode(pass);
            LevelDAOSchool levelDAO = new LevelDAOSchool();
            user.Level = levelDAO.getByName("ADMIN");
            userDAO.save(user);
        }

        public void closeForm(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }


    }
}
